<%@ page import="java.sql.*" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="db.jsp" %>
<html>
<head>
    <%
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Object oname = session.getAttribute("name");
        if (oname == null) {
            request.setAttribute("msg", "请登录后查看");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
        String action = request.getParameter("action");
        String title = "";
        String id = "";
        Long sno = 0L;
        String name = "";
        String sex = "";
        String clazz = "";
        String homepath = "";
        String phone = "";
        String username = "";
        String password = "";
        switch (action) {
            case "view": {
                title = "查看学生";
            }
            case "edit": {
                id = request.getParameter("id");
                //4、创建sql语句（下达命令）
                String sql = "SELECT * from t_user t where t.id=" + id + " limit 0,1";
                ResultSet rs = stmt.executeQuery(sql);
                if (rs.next()) {
                    sno = Long.parseLong(rs.getString("sno"));
                    name = rs.getString("name");
                    sex = rs.getString("sex");
                    clazz = rs.getString("clazz");
                    homepath = rs.getString("home_path");
                    phone = rs.getString("phone");
                    username = rs.getString("username");
                    password = rs.getString("password");
                }
                title = "修改学生";
                break;
            }
            case "save": {

                id = request.getParameter("id");
                sno = Long.parseLong(request.getParameter("sno"));
                name = request.getParameter("name");
                sex = request.getParameter("sex");
                phone = request.getParameter("phone");
                username = request.getParameter("username");
                password = request.getParameter("password");
                clazz = request.getParameter("clazz");
                homepath = request.getParameter("homepath");
                PreparedStatement pstmt = null;
                String url = "";
                if (id == null || id.length() == 0) {
                    pstmt = connection.prepareStatement("insert into t_user(id,sno,name,sex,phone,username,password,clazz,home_path) values(0,?,?,?,?,?,?,?,?)");
                    url = "add";
                } else {
                    pstmt = connection.prepareStatement("UPDATE t_user set sno=?,name=?,sex=?,phone=?,username=?,password=?,clazz=?,home_path=? where id=?");
                    pstmt.setString(9, id);
                    url = "edit";
                }
                //4、创建sql语句（下达命令）
                pstmt.setLong(1, sno);
                pstmt.setString(2, name);
                pstmt.setString(3, sex);
                pstmt.setString(4, phone);
                pstmt.setString(5, username);
                pstmt.setString(6, password);
                pstmt.setString(7, clazz);
                pstmt.setString(8, homepath);
                try {
                    int count = pstmt.executeUpdate();
                    connection.close();
                    out.print("    <script>\n" +
                            "        alert(\"保存成功!\");\n" +
                            "        window.location=\"index.jsp\";\n" +
                            "    </script>");
                } catch (Exception e) {
                    request.setAttribute("id", id);
                    request.setAttribute("name", name);
                    request.setAttribute("sex", sex);
                    request.setAttribute("phone", phone);
                    request.setAttribute("username", username);
                    request.setAttribute("password", password);
                    request.setAttribute("clazz", clazz);
                    request.setAttribute("homepath", homepath);
                    request.setAttribute("msg", e.getLocalizedMessage());
                    connection.close();
                    request.getRequestDispatcher("student.jsp?action=" + url).forward(request, response);
                }
                break;
            }
            case "delete": {
                request.setCharacterEncoding("UTF-8");
                id = request.getParameter("id");
                String sql = "DELETE FROM  t_user where id in (" + id + ")";
                try {
                    int count = stmt.executeUpdate(sql);
                    connection.close();
                    if (count > 0) {
                        out.print("    <script>\n" +
                                "        alert(\"删除成功!\");\n" +
                                "        window.location=\"index.jsp\";\n" +
                                "    </script>");
                    } else {
                        out.print("    <script>\n" +
                                "        alert(\"删除失败，没有找到该学号的学生!\");\n" +
                                "        window.location=\"index.jsp\";\n" +
                                "    </script>");
                    }
                } catch (Exception e) {
                    out.print("    <script>\n" +
                            "        alert(\"删除失败：" + e.getLocalizedMessage() + "\");\n" +
                            "        window.location=\"index.jsp\";\n" +
                            "    </script>");
                }
                break;
            }
            default: {
                title = "添加学生";
                String sql = "SELECT MAX(sno)+1 as maxsno from t_user";
                ResultSet rs = stmt.executeQuery(sql);
                if (rs.next()) {
                    sno = rs.getLong("maxsno");
                }
                break;
            }

        }
    %>
    <title><%=title%>
    </title>


</head>
<body>


<center>
    <form action="student.jsp?action=save" method="post">
        <span style="color: red;font-weight: bold">${msg}</span>
        <table border="1px">
            <tr>
                <td>学号</td>
                <input type="hidden" name="id" value="<%=id%>">
                <td><input type="text" name="sno" value="<%=sno%>" readonly></td>
            </tr>
            <tr>
                <td>姓名</td>
                <td><input type="text" name="name" id="name" value="<%=name%>"></td>
            </tr>
            <tr>
                <td>性别</td>
                <td>
                    <select name="sex" id="sex">
                        <option value="男" <%="男".equals(sex) ? "selected" : ""%>>男</option>
                        <option value="女" <%="女".equals(sex) ? "selected" : ""%>>女</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>电话号码</td>
                <td><input type="text" name="phone" id="phone" value="<%=phone%>"></td>
            </tr>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="username" id="username" value="<%=username%>"></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="password" id="password" value="<%=password%>"></td>
            </tr>
            <tr>
                <td>班级</td>
                <td><input type="text" name="clazz" id="clazz" value="<%=clazz%>"></td>
            </tr>
            <tr>
                <td>根目录</td>
                <td><input type="text" name="homepath" id="homepath" value="<%=homepath%>"></td>
            </tr>
            <tr>
                <%
                    if ("view".equals(action)) {
                %>
                <td colspan="2" align="center">
                    <a href="index.jsp">返回</a>
                </td>
                <%
                } else {
                %>
                <td><input type="submit" value="保存"></td>
                <td><input type="reset" value="重置"></td>
                <%
                    }
                %>
            </tr>
        </table>
    </form>
</center>
<%
    connection.close();
%>

</body>
</html>
